package com.example.tuter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tuter.R;
import com.example.tuter.entity.Notification;
import com.example.tuter.util.TimeUnitUtil;

import java.util.ArrayList;

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.UnreadViewHolder> {

    private ArrayList<Notification> data;
    private Context context;
    private final int TYPE_ITEM = 1;
    private final int TYPE_FOOTER = 2;

    public NotificationAdapter(ArrayList<Notification> data, Context context) {
        this.data = data;
        this.context = context;
    }
    @NonNull
    @Override
    public UnreadViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_notificaton_2, parent, false);
            return new NotificationAdapter.UnreadViewHolder(view, viewType);

        } else if (viewType == TYPE_FOOTER) {
            View footview = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.no_content_more, parent, false);
            return new NotificationAdapter.UnreadViewHolder(footview, viewType);
        }
        return null;
    }
    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull UnreadViewHolder holder, int position) {
        if (getItemViewType(position)==TYPE_ITEM) {
            Notification notification = data.get(position);
            holder.username.setText(notification.getUsername());
            holder.content.setText(notification.getContent());
            String time = "丢了";
            try {
                time = TimeUnitUtil.getTimeByTimestampString(Long.valueOf(notification.getCreateTime()));
            }catch (Exception ignored){

            }
            holder.time.setText(time);
        }
    }


    public class UnreadViewHolder extends RecyclerView.ViewHolder {

        private TextView username;
        private TextView content;
        private TextView time;

        LinearLayout footView;
        public UnreadViewHolder(@NonNull View itemView,int viewType) {
            super(itemView);
            if (viewType == TYPE_ITEM) {
                username = itemView.findViewById(R.id.unread_username);
                content = itemView.findViewById(R.id.unread_content);
                time = itemView.findViewById(R.id.unread_convert_time);
            } else if (viewType == TYPE_FOOTER) {
                footView = (LinearLayout) itemView;
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
    private OnRecyclerItemClickListener monItemClickListener;
    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener){
        monItemClickListener=listener;
    }
    public interface OnRecyclerItemClickListener{
        void onRecyclerItemClick(int position);
    }
}
